﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text.RegularExpressions;
public partial class Manage_Emp3_Record3Detail : AdminPageBase
{
    DataSet dsRecord;
    int id;
	private string returnURL;
    string emplevelId = "0";
    string teacherTypeId = "0";
    string TeacherCode = "";
    string TeacherName = "";
    string Time = "";
    string Satisficing = "";
    string FactTimes = "";
    string OpenTime = "";
       
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckAdmin();
        GetUserPermisions();
        GetClassRecordId();
		returnURL = Request.QueryString["returnURL"].ToString();
        if (ViewState["tbRecord"] == null)
        {
            dsRecord = tbRecord();

            ViewState["tbRecord"] = dsRecord;
        }
        else
            dsRecord = (DataSet)ViewState["tbRecord"];

        if (!IsPostBack)
        {
            // 正确的属性设置方法
            this.GridView1.Attributes.Add("SortExpression", "RecordId");
            this.GridView1.Attributes.Add("SortDirection", "ASC");
            this.GridView2.Attributes.Add("SortExpression", "RecordId");
            this.GridView2.Attributes.Add("SortDirection", "ASC");
            this.GridView3.Attributes.Add("SortExpression", "ClassId");
            this.GridView3.Attributes.Add("SortDirection", "ASC");
            this.grdvCity.Attributes.Add("SortExpression", "CityId");
            this.grdvCity.Attributes.Add("SortDirection", "ASC");
           
            DataTable dt = new DataTable();
            dt.Columns.Add("RecordId");
            dt.Columns.Add("ClassTypeName");
            dt.Columns.Add("ClassCode");
            dt.Columns.Add("ClassName");
            dt.Columns.Add("ClassId");
            dt.Columns.Add("Satisficing");
            dt.Columns.Add("TeacherName");
            dt.Columns.Add("TeacherCode");
            dt.Columns.Add("TeacherTypeId");
            dt.Columns.Add("TeacherTypeName");
            dt.Columns.Add("EmpLevelId");
            dt.Columns.Add("EmpLevelName");
            dt.Columns.Add("Time");
            dt.Columns.Add("OpenTime");
            dt.Columns.Add("FactTimes");
            dt.Columns.Add("CenterName");
            dt.Columns.Add("CenterId");
            dt.Columns.Add("CityId");
            dt.Columns.Add("CityName");

            dt.Columns.Add("");

            GridviewControl.GridViewDataBind(this.GridView2, dt);
            GridviewControl.ResetGridView(this.GridView2);
            BindRecord();
        }
    }

    protected void GetClassRecordId()
    {
        if (Request.QueryString["id"] == null)
        {
            id = 0;

        }
        else
        {
            id = Convert.ToInt32(Request.QueryString["id"]);

        }
        if (Request.QueryString["Num"] == null)
        {
            lblNum.Text = "";
        }
        else
            lblNum.Text = Request.QueryString["Num"].ToString();
        if (Request.QueryString["Title"] == null)
            txttitle.Text = "";
        else
            txttitle.Text = Request.QueryString["Title"].ToString();
    }
    protected void CheckAdmin()
    {
        if (AdminSessionHelper.UserDetails == null)
        {
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }
    }
    protected void BindRecord()
    {

        if (id != 0)
        {
            DataSet dsemp = jtDataAccess.GetGetRecordByClassRecordId(id);
            if (CoreTools.CheckDataSet(dsemp))
            {
                // 获取GridView排序数据列及排序方向
                string sortExpression = this.GridView1.Attributes["SortExpression"];
                string sortDirection = this.GridView1.Attributes["SortDirection"];

                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    dsemp.Tables[0].DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }

                // GridView绑定并显示数据
                GridviewControl.GridViewDataBind(this.GridView1, dsemp.Tables[0]);
         

            }
            else
            {
                DataTable dt = new DataTable();
                dt.Columns.Add("RecordId");
                dt.Columns.Add("ClassTypeName");
                dt.Columns.Add("ClassCode");
                dt.Columns.Add("ClassName");
                dt.Columns.Add("ClassId");
                dt.Columns.Add("Satisficing");
                dt.Columns.Add("TeacherName");
                dt.Columns.Add("TeacherCode");
                dt.Columns.Add("TeacherTypeId");
                dt.Columns.Add("TeacherTypeName");
                dt.Columns.Add("EmpLevelId");
                dt.Columns.Add("EmpLevelName");
                dt.Columns.Add("Time");
                dt.Columns.Add("OpenTime");
                dt.Columns.Add("FactTimes");
                dt.Columns.Add("CenterName");
                dt.Columns.Add("CenterId");
                dt.Columns.Add("CityId");
                dt.Columns.Add("CityName");

                dt.Columns.Add("");

                GridviewControl.GridViewDataBind(this.GridView1, dt);
          
            
            }
        }
    }
   
    protected void btnsave_Click(object sender, EventArgs e)
    {
        int flag = SaveClassRecord();
        switch (flag)
        {
            case -1:
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败，已经存在相同编号的课程！"), false);
                break;
            case 0:
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败！"), false);
                break;
            default:
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作成功！"), false);
                break;
        }
        GridviewControl.ResetGridView(this.GridView1);
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected int SaveClassRecord()
    {
        int flag = 0;
        string Num = lblNum.Text;
        string title = txttitle.Text.Trim();
        if (!CoreTools.ProcessSqlStr(title, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txttitle.Focus();
        }
        else
        {
            flag = jtDataAccess.OpClassRecord(JTDataAccess.OpType.Update, id, 3, Num, title, int.Parse(AdminSessionHelper.UserDetails["UserId"].ToString(),0), 0);

        }
        return flag;

    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindRecord();
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            LinkButton BtnDelete = (LinkButton)e.Row.Cells[13].Controls[2];
            if (BtnDelete.Text == "删除")
            {
                ((LinkButton)e.Row.Cells[13].Controls[2]).Attributes.Add("onclick", "javascript:return confirm('您确定要删除该记录吗？')");
            }

        }
        
        DropDownList ddlEmpLevel = (DropDownList)e.Row.FindControl("ddlEmpLevel");
        HiddenField hdfEmpLevelId = (HiddenField)e.Row.FindControl("hdfEmpLevelId");
        DropDownList ddlTeacherType = (DropDownList)e.Row.FindControl("ddlTeacherType");
        HiddenField hdfTeacherTypeId = (HiddenField)e.Row.FindControl("hdfTeacherTypeId");
        if (ddlEmpLevel != null)
        {
            DataSet ds = jtDataAccess.SearchEmpLevel("", 10, 0, "Name", "ASC");
            if (CoreTools.CheckDataSet(ds))
            {
                ddlEmpLevel.DataSource = ds;
                ddlEmpLevel.DataTextField = "Name";
                ddlEmpLevel.DataValueField = "EmpLevelId";
                ddlEmpLevel.DataBind();
            }
            if (hdfEmpLevelId != null && hdfEmpLevelId.Value != "")
                ddlEmpLevel.SelectedValue = hdfEmpLevelId.Value;

        }
        if (ddlTeacherType != null)
        {
            DataSet ds = jtDataAccess.SearchTeacherType("", 10, 0, "Name", "ASC");
            if (CoreTools.CheckDataSet(ds))
            {
                ddlTeacherType.DataSource = ds;
                ddlTeacherType.DataTextField = "Name";
                ddlTeacherType.DataValueField = "TeacherTypeId";
                ddlTeacherType.DataBind();
            }
            if (hdfTeacherTypeId != null && hdfTeacherTypeId.Value != "")
                ddlTeacherType.SelectedValue = hdfTeacherTypeId.Value;

        }
        if (edit == 1)
            ((CommandField)GridView1.Columns[13]).ShowEditButton = true;
        else
            ((CommandField)GridView1.Columns[13]).ShowEditButton = false;
        if (delete == 1)
            ((CommandField)GridView1.Columns[13]).ShowDeleteButton = true;
        else
            ((CommandField)GridView1.Columns[13]).ShowDeleteButton = false;

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        UserType ut = new UserType();//用户自定义类型
        DataTable RecordTable = ut.GetRecordTableType();

        DataRow RecordTableRow;
        int RecordId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString());


        RecordTableRow = RecordTable.NewRow();
        RecordTableRow["RecordId"] = RecordId;
        RecordTable.Rows.Add(RecordTableRow);

        RecordTable.AcceptChanges();
        int flag = jtDataAccess.OpRecord(JTDataAccess.OpType.Delete, RecordTable);
        switch (flag)
        {
            case 0:
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("删除失败！"), false);
                break;

            case 1:
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("删除成功！"), false);
                break;
            default:
                break;
        }

        GridView1.EditIndex = -1;
        BindRecord();
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindRecord();
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {

        int flag = 0;
        int RecordId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
        int classid = Convert.ToInt32(((HiddenField)GridView1.Rows[e.RowIndex].FindControl("hdfClassId")).Value);
        int centerId = Convert.ToInt32(((HiddenField)GridView1.Rows[e.RowIndex].FindControl("hdfCenterId")).Value);
        int cityId = Convert.ToInt32(((HiddenField)GridView1.Rows[e.RowIndex].FindControl("hdfcityId")).Value);
        int TeacherTypeId = Convert.ToInt32(((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlTeacherType")).SelectedValue);
        int EmpLevelId = Convert.ToInt32(((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlEmpLevel")).SelectedValue);
        int Satisficing = -1;
        string TeacherName = "";
        decimal time = 0;
        int FactTimes = 0;
        string opentime = "";
        string teachercode = "";
        TextBox cSatisficing = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtSatisficing");
        TextBox cTeacherName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTeacherName");
        TextBox cTeacherCode = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTeacherCode");
        TextBox ctime = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTime");
        TextBox copentime = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtOpenTime");
        TextBox cFactTimes = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtFactTimes");
        if (cSatisficing.Text == "")
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请填写课程满意程度！"), false);
            cSatisficing.Focus();
        }
        else if (!Regex.IsMatch(cSatisficing.Text.ToString(), "^(?:0|[1-9][0-9]?|100)$"))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("课程满意程度只能为0到100的正数！"), false);
            cSatisficing.Focus();
        }
        else
            Satisficing = Convert.ToInt32(cSatisficing.Text.Trim());

        if (cTeacherName.Text == "")
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请填写导师姓名！"), false);
            cTeacherName.Focus();
        }
        else if (!CoreTools.ProcessSqlStr(TeacherName, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
        }
        else
            TeacherName = cTeacherName.Text.Trim();
        if (cTeacherCode.Text == "")
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请填写导师编号或选择导师！"), false);
            cTeacherCode.Focus();
        }
        else if (!CoreTools.ProcessSqlStr(teachercode, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
        }
        else
            teachercode = cTeacherCode.Text.Trim();
        if (ctime.Text == "")
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请填写课程小时数！"), false);
            ctime.Focus();
        }
        else
            time = Convert.ToDecimal(ctime.Text.Trim());
        if (cFactTimes.Text == "")
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请填写培训人次！"), false);
            cFactTimes.Focus();
        }
        else if (!Regex.IsMatch(cFactTimes.Text.ToString(), "^[0-9]*[1-9][0-9]*$"))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("培训人次必须为正整数！"), false);
            cFactTimes.Focus();
        }
        else
            FactTimes = Convert.ToInt32(cFactTimes.Text.Trim());
        if (copentime.Text == "")
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请填写开课时间！"), false);
            copentime.Focus();
        }
        else if (!Regex.IsMatch(copentime.Text.ToString(), "^([2-9]\\d{3})(0[1-9]|1[0-2])$"))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("开课时间格式不正确,正确格式形如：201206！"), false);
            copentime.Focus();
        }
        else
            opentime = copentime.Text.Trim();


        if (Satisficing != -1 && TeacherName != "" &&teachercode!=""&& time != 0 && opentime != "" && FactTimes != 0)
        {
            UserType ut = new UserType();//用户自定义类型
            DataTable RecordTable = ut.GetRecordTableType();
            DataRow RecordTableRow;
            RecordTableRow = RecordTable.NewRow();
            RecordTableRow["RecordId"] = RecordId;
            RecordTableRow["ClassRecordId"] = id;
            RecordTableRow["EmpTypeId"] = 3;
            RecordTableRow["ClassId"] = classid;
            RecordTableRow["Satisficing"] = Satisficing;
            RecordTableRow["TeacherName"] = TeacherName;
            RecordTableRow["Time"] = time;
            RecordTableRow["FactTimes"] =FactTimes;
            RecordTableRow["CenterId"] = centerId;
            RecordTableRow["OpenTime"] = opentime;
            RecordTableRow["TeacherTypeId"] = TeacherTypeId;
            RecordTableRow["TeacherCode"] =teachercode;
            RecordTableRow["EmpLevelId"] =EmpLevelId;
            RecordTableRow["CityId"] =cityId;
            RecordTable.Rows.Add(RecordTableRow);
            flag = jtDataAccess.OpRecord(JTDataAccess.OpType.Update, RecordTable);
            switch (flag)
            {
                case 1:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作成功！"), false);
                    break;
                case 0:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败，请重试！"), false);
                    break;
                default:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作成功！"), false);

                    break;
            }
            GridView1.EditIndex = -1;
            BindRecord();
        }
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    {
        // 从事件参数获取排序数据列
        string sortExpression = e.SortExpression.ToString();

        // 假定为排序方向为“顺序”
        string sortDirection = "ASC";

        // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
        if (sortExpression == this.GridView1.Attributes["SortExpression"])
        {
            //获得下一次的排序状态
            sortDirection = (this.GridView1.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
        }

        // 重新设定GridView排序数据列及排序方向
        this.GridView1.Attributes["SortExpression"] = sortExpression;
        this.GridView1.Attributes["SortDirection"] = sortDirection;

        this.BindRecord();
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void btnMassAdd_Click(object sender, EventArgs e)
    {
        UserType ut = new UserType();//用户自定义类型
        DataTable RecordTable = ut.GetRecordTableType();
        if (GridView2.EditIndex != -1)
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("还有数据项处于编辑状态，请完成编辑再添加！"), false);
        }
        else
        {
            int check =0;
          
            DataRow RecordTableRow;
            for (int i = 0; i < GridView2.Rows.Count; i++)
            {
                CheckBox cbox = (CheckBox)GridView2.Rows[i].FindControl("chkID1");
                HiddenField hdfClassId = (HiddenField)GridView2.Rows[i].FindControl("hdfClassId");
                HiddenField hdfcityId = (HiddenField)GridView2.Rows[i].FindControl("hdfcityId");
                HiddenField hdfCenterId = (HiddenField)GridView2.Rows[i].FindControl("hdfCenterId");
                HiddenField hdfTeacherTypeId = (HiddenField)GridView2.Rows[i].FindControl("hdfTeacherTypeId");
                HiddenField hdfEmpLevelId = (HiddenField)GridView2.Rows[i].FindControl("hdfEmpLevelId");
                Label lblSatisficing = (Label)GridView2.Rows[i].FindControl("lblSatisficing");
                Label lblTeacherCode = (Label)GridView2.Rows[i].FindControl("lblTeacherCode");
                Label lblTeacherName = (Label)GridView2.Rows[i].FindControl("lblTeacherName");
                Label lblFactTimes = (Label)GridView2.Rows[i].FindControl("lblFactTimes");
                Label lblOpenTime = (Label)GridView2.Rows[i].FindControl("lblOpenTime");
                decimal time = Convert.ToDecimal(((Label)GridView2.Rows[i].FindControl("lblTime")).Text.Trim());
                if (cbox.Checked == true && hdfClassId != null && hdfClassId.Value != ""&&hdfClassId.Value!="0")
                {

                    if (lblSatisficing.Text == "" || lblTeacherName.Text == "" || time == 0 || lblTeacherCode.Text == "" || lblFactTimes.Text == "" ||
                        hdfCenterId.Value == "0" || lblOpenTime.Text == "" || hdfTeacherTypeId.Value == "0" || hdfEmpLevelId.Value == "0" || hdfcityId.Value == "0")
                    {
                        check += 1;
                        cbox.Checked = false;
                        i++;
                    }
                    else
                    {
                        RecordTableRow = RecordTable.NewRow();
                        RecordTableRow["RecordId"] = 0;
                        RecordTableRow["ClassRecordId"] = id;
                        RecordTableRow["EmpTypeId"] = 3;
                        RecordTableRow["ClassId"] = Convert.ToInt32(hdfClassId.Value);
                        RecordTableRow["Satisficing"] = lblSatisficing.Text;
                        RecordTableRow["TeacherName"] = lblTeacherName.Text;
                        RecordTableRow["TeacherCode"] = lblTeacherCode.Text;
                        RecordTableRow["Time"] = time;
                        RecordTableRow["FactTimes"] = lblFactTimes.Text;
                        RecordTableRow["CenterId"] = Convert.ToInt32(hdfCenterId.Value);
                        RecordTableRow["OpenTime"] = lblOpenTime.Text;
                        RecordTableRow["TeacherTypeId"] = Convert.ToInt32(hdfTeacherTypeId.Value);
                        RecordTableRow["EmpLevelId"] = Convert.ToInt32(hdfEmpLevelId.Value);
                        RecordTableRow["CityId"] = Convert.ToInt32(hdfcityId.Value);
                        RecordTable.Rows.Add(RecordTableRow);
                    }
                }
            }
            RecordTable.AcceptChanges();
            int flag = jtDataAccess.OpRecord(JTDataAccess.OpType.Insert, RecordTable);

            switch (flag)
            {
                case 0:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("添加失败！"), false);
                    break;

                case 1:
                    if(check==0)
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("添加成功！"), false);
                    else
                     ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("有"+check+"项没有添加成功，因为数据没有编辑完整！"), false);
                    BindRecord();
                    RemovedsRecord();
                    break;

                default:
                    break;
            }
            selectAll.Checked = false;

           
        }
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void selectAll_CheckedChanged(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView2.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView2.Rows[i].FindControl("chkID1");
            if (selectAll.Checked == true)
            {
                cbox.Checked = true;
            }
            else
            {
                cbox.Checked = false;
            }
        }
        GridviewControl.ResetGridView(this.GridView1);
    }
    protected void RemovedsRecord()
    {
        for (int i = 0; i <= GridView2.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView2.Rows[i].FindControl("chkID1");
            HiddenField hdfRecordId = (HiddenField)GridView2.Rows[i].FindControl("hdfRecordId");
            if (cbox.Checked == true && hdfRecordId != null && hdfRecordId.Value != "")
            {
                string newrecordid = hdfRecordId.Value;
                for (int j = 0; j < dsRecord.Tables[0].Rows.Count; j++)
                {
                    string reid=dsRecord.Tables[0].Rows[j]["RecordId"] .ToString();
                    if (reid == newrecordid)
                    {
                        dsRecord.Tables[0].Rows.Remove(dsRecord.Tables[0].Rows[j]);
                        break;
                    }
                }

            }
        }
        ViewState["tbRecord"] = dsRecord;
        GridView2.DataSource = dsRecord;
        GridView2.DataBind();
        if (GridView2.Rows.Count == 0)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("RecordId");
            dt.Columns.Add("ClassTypeName");
            dt.Columns.Add("ClassCode");
            dt.Columns.Add("ClassName");
            dt.Columns.Add("ClassId");
            dt.Columns.Add("Satisficing");
            dt.Columns.Add("TeacherName");
            dt.Columns.Add("TeacherCode");
            dt.Columns.Add("TeacherTypeId");
            dt.Columns.Add("TeacherTypeName");
            dt.Columns.Add("EmpLevelId");
            dt.Columns.Add("EmpLevelName");
            dt.Columns.Add("Time");
            dt.Columns.Add("OpenTime");
            dt.Columns.Add("FactTimes");
            dt.Columns.Add("CenterName");
            dt.Columns.Add("CenterId");
            dt.Columns.Add("CityId");
            dt.Columns.Add("CityName");

            dt.Columns.Add("");

            GridviewControl.GridViewDataBind(this.GridView2, dt);
          
        }
        GridviewControl.ResetGridView(this.GridView1);
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void btnDeleteMass_Click(object sender, EventArgs e)
    {
        RemovedsRecord();
        
    }
    protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            DropDownList ddlEmpLevel = (DropDownList)e.Row.FindControl("ddlEmpLevel");
            HiddenField hdfEmpLevelId = (HiddenField)e.Row.FindControl("hdfEmpLevelId");
            DropDownList ddlTeacherType = (DropDownList)e.Row.FindControl("ddlTeacherType");
            HiddenField hdfTeacherTypeId = (HiddenField)e.Row.FindControl("hdfTeacherTypeId");
            if (ddlEmpLevel != null)
            {
                DataSet ds = jtDataAccess.SearchEmpLevel("", 10, 0, "Name", "ASC");
                if (CoreTools.CheckDataSet(ds))
                {
                    ddlEmpLevel.DataSource = ds;
                    ddlEmpLevel.DataTextField = "Name";
                    ddlEmpLevel.DataValueField = "EmpLevelId";
                    ddlEmpLevel.DataBind();
                }
                if (hdfEmpLevelId != null && hdfEmpLevelId.Value != "")
                    ddlEmpLevel.SelectedValue = hdfEmpLevelId.Value;

            }
            if (ddlTeacherType != null)
            {
                DataSet ds = jtDataAccess.SearchTeacherType("", 10, 0, "Name", "ASC");
                if (CoreTools.CheckDataSet(ds))
                {
                    ddlTeacherType.DataSource = ds;
                    ddlTeacherType.DataTextField = "Name";
                    ddlTeacherType.DataValueField = "TeacherTypeId";
                    ddlTeacherType.DataBind();
                }
                if (hdfTeacherTypeId != null && hdfTeacherTypeId.Value != "")
                    ddlTeacherType.SelectedValue = hdfTeacherTypeId.Value;

            }
       
        }
        
        ((CommandField)GridView2.Columns[14]).ShowEditButton = true;
        ((CommandField)GridView2.Columns[14]).ShowDeleteButton = true;
    }
    protected void GridView2_Sorting(object sender, GridViewSortEventArgs e)
    {
        // 从事件参数获取排序数据列
        string sortExpression = e.SortExpression.ToString();

        // 假定为排序方向为“顺序”
        string sortDirection = "ASC";

        // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
        if (sortExpression == this.GridView2.Attributes["SortExpression"])
        {
            //获得下一次的排序状态
            sortDirection = (this.GridView2.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
        }

        // 重新设定GridView排序数据列及排序方向
        this.GridView2.Attributes["SortExpression"] = sortExpression;
        this.GridView2.Attributes["SortDirection"] = sortDirection;

        GridviewControl.ResetGridView(this.GridView1);
    }

    protected void btnreturn_Click(object sender, EventArgs e)
    {
        //Response.Redirect("Record3.aspx?MenuId=" + MenuId);
		Response.Redirect(returnURL);
    }
    protected void btnadd_Click(object sender, EventArgs e)
    {
        this.GridView1.EditIndex = -1;
        BindRecord();
        if (ViewState["tbRecord"] != null)
            dsRecord =(DataSet) ViewState["tbRecord"];
        int NewRecordId = 0;
        NewRecordId = dsRecord.Tables[0].Rows.Count + 1;
        DataRow row =dsRecord.Tables[0].NewRow();
        row["RecordId"] = NewRecordId;
        row["CityId"] = 0;
        row["CityName"] = "";
        row["CenterId"] = 0;
        row["CenterName"] = "";
        row["TeacherTypeId"] = 0;
        row["TeacherTypeName"] = "";
        row["TeacherCode"] = "";
        row["TeacherName"] = "";
        row["EmpLevelId"] = 0;
        row["EmpLevelName"] = "";
        row["ClassId"] = 0;
        row["ClassTypeName"] = "";
        row["ClassCode"] = "";
        row["ClassName"] = "";
        row["Time"] = 0;
        row["OpenTime"] = "";
        row["FactTimes"] = 0;
        dsRecord.Tables[0].Rows.Add(row);
        ViewState["tbRecord"] = dsRecord;
        this.GridView2.DataSource = dsRecord;
        this.GridView2.DataBind();
        this.GridView2.EditIndex = this.GridView2.Rows.Count - 1;
        this.GridView2.DataBind();
    }
    protected void BindClass()
    {
        // 获取GridView排序数据列及排序方向
        string sortExpression = this.GridView3.Attributes["SortExpression"];
        string sortDirection = this.GridView3.Attributes["SortDirection"];

        string keyword = txtkey.Text.Trim();
        if (CoreTools.ProcessSqlStr(keyword, 0))
        {
            DataSet ds = jtDataAccess.SearchClass(keyword, pager2.PageSize, pager2.CurrentPageIndex, sortExpression, sortDirection);
            if (CoreTools.CheckDataSet(ds))
            {
                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    ds.Tables[0].DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }

                // GridView绑定并显示数据
                this.GridView3.DataSource = ds.Tables[0];
                this.GridView3.DataBind();
                pager2.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0]["qty"]);
            }
            else
            {
                this.GridView3.DataSource = null;
                this.GridView3.DataBind();
                pager2.RecordCount = 0;
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtkey.Focus();
        }

    }
    protected void GridView3_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

        }
    }
    protected void GridView3_Sorting(object sender, GridViewSortEventArgs e)
    {
        // 从事件参数获取排序数据列
        string sortExpression = e.SortExpression.ToString();

        // 假定为排序方向为“顺序”
        string sortDirection = "ASC";

        // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
        if (sortExpression == this.GridView3.Attributes["SortExpression"])
        {
            //获得下一次的排序状态
            sortDirection = (this.GridView3.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
        }

        // 重新设定GridView排序数据列及排序方向
        this.GridView3.Attributes["SortExpression"] = sortExpression;
        this.GridView3.Attributes["SortDirection"] = sortDirection;

        this.BindClass();
    }
   
    protected void ibtnsearch_Click(object sender, ImageClickEventArgs e)
    {
        grdvTeacher.EditIndex = -1;
        BindTeacher();
    }
    protected void BindTeacher()
    {
        string keyword = txtkeyword.Text.Trim();
        if (CoreTools.ProcessSqlStr(keyword, 0))
        {
            DataSet ds = jtDataAccess.FilterTeacher(keyword,0);
            if (CoreTools.CheckDataSet(ds))
            {
                
                // GridView绑定并显示数据
                this.grdvTeacher.DataSource = ds.Tables[0];
                this.grdvTeacher.DataBind();
                
            }
            else
            {
                this.grdvTeacher.DataSource = null;
                this.grdvTeacher.DataBind();
               

            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtkeyword.Focus();
        }
    }
    protected void BindCity()
    {
        // 获取GridView排序数据列及排序方向
        string sortExpression = this.grdvCity.Attributes["SortExpression"];
        string sortDirection = this.grdvCity.Attributes["SortDirection"];

        string keyword = txtckey.Text.Trim();
        if (CoreTools.ProcessSqlStr(keyword, 0))
        {
            DataSet ds = jtDataAccess.SearchCity(keyword, pager4.PageSize, pager4.CurrentPageIndex, sortExpression, sortDirection);
            if (CoreTools.CheckDataSet(ds))
            {
                // 根据GridView排序数据列及排序方向设置显示的默认数据视图
                if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
                {
                    ds.Tables[0].DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
                }

                // GridView绑定并显示数据
                this.grdvCity.DataSource = ds.Tables[0];
                this.grdvCity.DataBind();
                pager4.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0]["qty"]);
            }
            else
            {
                this.grdvCity.DataSource = null;
                this.grdvCity.DataBind();
                pager4.RecordCount = 0;
            }
        }
        else
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtckey.Focus();
        }

    }
    protected void ibtnSearchClass_Click(object sender, ImageClickEventArgs e)
    {
        GridView3.EditIndex = -1;
       
        BindClass();
    }
    protected void pager2_PageChanged(object sender, EventArgs e)
    {
        GridView3.EditIndex = -1;
        BindClass();
    }
   
    protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "sel")
        {
            int editflag = Convert.ToInt32(hdfeditflag.Value);
            if (editflag == 0)
            {
                GetHaveSetData();
                GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 
                Label lblTypename = (Label)(drv.FindControl("lblTypename"));
                Label lblTime = (Label)(drv.FindControl("lblTime"));
                Label lblCode = (Label)(drv.FindControl("lblCode"));
                Label lblname = (Label)(drv.FindControl("lblname"));
                if (ViewState["tbRecord"] != null)
                    dsRecord = (DataSet)ViewState["tbRecord"];
                int i = this.GridView2.EditIndex;
                dsRecord.Tables[0].Rows[i]["ClassId"] = e.CommandArgument.ToString();
                dsRecord.Tables[0].Rows[i]["ClassTypeName"] = lblTypename.Text;
                dsRecord.Tables[0].Rows[i]["Time"] = lblTime.Text;
                Time = lblTime.Text;
                dsRecord.Tables[0].Rows[i]["ClassName"] = lblname.Text;
                dsRecord.Tables[0].Rows[i]["ClassCode"] = lblCode.Text;
                this.GridView2.DataSource = dsRecord;
                this.GridView2.DataBind();
                ViewState["tbRecord"] = dsRecord;
                divSClass.Visible = false;
                floatBoxBg2.Visible = false;
                SetHaveSetData();               
            }
            else
            {
               
                GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 
                Label lblTypename = (Label)(drv.FindControl("lblTypename"));
                Label lblTime = (Label)(drv.FindControl("lblTime"));
                Label lblCode = (Label)(drv.FindControl("lblCode"));
                Label lblname = (Label)(drv.FindControl("lblname"));
                Label lblIsValid = (Label)(drv.FindControl("lblIsValid"));

                Label lblClassTypeName = (Label)GridView1.Rows[GridView1.EditIndex].FindControl("lblClassTypeName");
                TextBox txtClassCode = (TextBox)GridView1.Rows[GridView1.EditIndex].FindControl("txtClassCode");
                HiddenField hdfClassId = (HiddenField)GridView1.Rows[GridView1.EditIndex].FindControl("hdfClassId");
                Label lblClassName = (Label)GridView1.Rows[GridView1.EditIndex].FindControl("lblClassName");
                TextBox txtTime = (TextBox)GridView1.Rows[GridView1.EditIndex].FindControl("txtTime");

                lblClassTypeName.Text = lblTypename.Text;
                txtClassCode.Text = lblCode.Text;
                lblClassName.Text = lblname.Text;
                txtTime.Text = lblTime.Text;
                hdfClassId.Value = e.CommandArgument.ToString();
                divSClass.Visible = false;
                floatBoxBg2.Visible = false;
                

            }
            GridviewControl.ResetGridView(this.GridView1);
            GridviewControl.ResetGridView(this.GridView2);

        }

    }
    protected void ibtnsearchCity_Click(object sender, ImageClickEventArgs e)
    {
        this.grdvCity.EditIndex = -1;

        BindCity();
    }
    protected void ibtncloseCitySelect_Click(object sender, ImageClickEventArgs e)
    {
        floatBoxBg3.Visible = false;
        divSCity.Visible=false;

    }
    protected void ibtncloseClassSelect_Click(object sender, ImageClickEventArgs e)
    {
        floatBoxBg2.Visible = false;
        divSClass.Visible = false;

    }
    protected void grdvCity_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "sel")
        {
            int editflag = Convert.ToInt32(hdfeditflag.Value);
            if (editflag == 0)
            {
                GetHaveSetData();
                GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 
                Label lblCenterName = (Label)(drv.FindControl("lblCenterName"));
                Label lblname = (Label)(drv.FindControl("lblname"));
                HiddenField hdfcenterId = (HiddenField)(drv.FindControl("hdfcenterId"));
                if (ViewState["tbRecord"] != null)
                    dsRecord = (DataSet)ViewState["tbRecord"];
                int i = this.GridView2.EditIndex;
                dsRecord.Tables[0].Rows[i]["CityId"] = e.CommandArgument.ToString();
                dsRecord.Tables[0].Rows[i]["CityName"] = lblname.Text;
                dsRecord.Tables[0].Rows[i]["CenterName"] = lblCenterName.Text;
                dsRecord.Tables[0].Rows[i]["CenterId"] = hdfcenterId.Value;
                ViewState["tbRecord"] = dsRecord;
                this.GridView2.DataSource = dsRecord;
                this.GridView2.DataBind();
                divSCity.Visible = false;
                floatBoxBg3.Visible = false;
                SetHaveSetData();
            }
            else
            {
               
                GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 
                Label lblCName = (Label)(drv.FindControl("lblCenterName"));
                Label lblname = (Label)(drv.FindControl("lblname"));
                HiddenField hdfcenterId = (HiddenField)(drv.FindControl("hdfcenterId"));

                Label lblCenterName = (Label)GridView1.Rows[GridView1.EditIndex].FindControl("lblCenterName");
                HiddenField hdfcityId = (HiddenField)GridView1.Rows[GridView1.EditIndex].FindControl("hdfcityId");
                TextBox txtcityName = (TextBox)GridView1.Rows[GridView1.EditIndex].FindControl("txtcityName");
                HiddenField hdfCenterId = (HiddenField)GridView1.Rows[GridView1.EditIndex].FindControl("hdfCenterId");
                lblCenterName.Text = lblCName.Text;
                hdfCenterId.Value = hdfcenterId.Value;
                hdfcityId.Value = e.CommandArgument.ToString();
                txtcityName.Text = lblname.Text;
                divSCity.Visible = false;
                floatBoxBg3.Visible = false;
                
            }
            GridviewControl.ResetGridView(this.GridView1);
            GridviewControl.ResetGridView(this.GridView2);

        }

    }
    protected void grdvCity_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

        }
    }
    protected void grdvCity_Sorting(object sender, GridViewSortEventArgs e)
    {
        // 从事件参数获取排序数据列
        string sortExpression = e.SortExpression.ToString();

        // 假定为排序方向为“顺序”
        string sortDirection = "ASC";

        // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
        if (sortExpression == this.grdvCity.Attributes["SortExpression"])
        {
            //获得下一次的排序状态
            sortDirection = (this.grdvCity.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
        }

        // 重新设定GridView排序数据列及排序方向
        this.grdvCity.Attributes["SortExpression"] = sortExpression;
        this.grdvCity.Attributes["SortDirection"] = sortDirection;

        this.BindCity();
    }
    protected void pager4_PageChanged(object sender, EventArgs e)
    {
       this.grdvCity.EditIndex = -1;
        BindCity();
    }
   
    protected void grdvTeacher_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "sel")
        {
            int editflag = Convert.ToInt32(hdfeditflag.Value);
            if (editflag == 0)
            {
                GetHaveSetData();
                
               
                GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 
                Label lblcode = (Label)(drv.FindControl("lblcode"));
                Label lblname = (Label)(drv.FindControl("lblname"));
              
                if (ViewState["tbRecord"] != null)
                    dsRecord = (DataSet)ViewState["tbRecord"];
                int i = this.GridView2.EditIndex;
                dsRecord.Tables[0].Rows[i]["TeacherName"] = lblname.Text;
                dsRecord.Tables[0].Rows[i]["TeacherCode"] = lblcode.Text;
                TeacherCode =lblcode.Text;
                TeacherName = lblname.Text;
                ViewState["tbRecord"] = dsRecord;
                this.GridView2.DataSource = dsRecord;
                this.GridView2.DataBind();
                divSTeacher.Visible = false;
                floatBoxBg1.Visible = false;
                SetHaveSetData();
            }
            else
            {
                GridViewRow drv = ((GridViewRow)(((LinkButton)(e.CommandSource)).Parent.Parent)); //此得出的值是表示那行被选中的索引值 
                Label lblcode = (Label)(drv.FindControl("lblcode"));
                Label lblname = (Label)(drv.FindControl("lblname"));

                TextBox txtTeacherCode = (TextBox)GridView1.Rows[GridView1.EditIndex].FindControl("txtTeacherCode");
                TextBox txtTeacherName = (TextBox)GridView1.Rows[GridView1.EditIndex].FindControl("txtTeacherName");
                txtTeacherCode.Text = lblcode.Text;
                txtTeacherName.Text = lblname.Text;
                divSTeacher.Visible = false;
                floatBoxBg1.Visible = false;
                
            }
            GridviewControl.ResetGridView(this.GridView1);
            GridviewControl.ResetGridView(this.GridView2);

        }
    }
    protected void grdvTeacher_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //首先判断是否是数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //当鼠标停留时更改背景色
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#FCFDEE'");
            //当鼠标移开时还原背景色
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");

        }
    }
  
    protected void ibtncloseTeacherSelect_Click(object sender, ImageClickEventArgs e)
    {
        divSTeacher.Visible = false;
        
        floatBoxBg1.Visible = false;
    }

    protected DataSet tbRecord()
    {
        DataSet ds = new DataSet();

        ds.Tables.Add();
        ds.Tables[0].TableName = "tb";
        ds.Tables[0].Columns.Add("RecordId", typeof(int));
        ds.Tables[0].Columns.Add("CityId", typeof(int));
        ds.Tables[0].Columns.Add("CityName", typeof(string));
        ds.Tables[0].Columns.Add("CenterName", typeof(string));
        ds.Tables[0].Columns.Add("CenterId", typeof(int));
        ds.Tables[0].Columns.Add("TeacherTypeId", typeof(int));
        ds.Tables[0].Columns.Add("TeacherTypeName", typeof(string));
        ds.Tables[0].Columns.Add("TeacherCode", typeof(string));
        ds.Tables[0].Columns.Add("TeacherName", typeof(string));
        ds.Tables[0].Columns.Add("EmpLevelId", typeof(int));
        ds.Tables[0].Columns.Add("EmpLevelName", typeof(string));
        
        ds.Tables[0].Columns.Add("ClassId", typeof(int));
        ds.Tables[0].Columns.Add("ClassTypeName", typeof(string));
        ds.Tables[0].Columns.Add("ClassCode", typeof(string));
        ds.Tables[0].Columns.Add("ClassName", typeof(string));
        ds.Tables[0].Columns.Add("Time", typeof(decimal));
        ds.Tables[0].Columns.Add("OpenTime", typeof(string));
        ds.Tables[0].Columns.Add("FactTimes", typeof(int));
        ds.Tables[0].Columns.Add("Satisficing", typeof(int));
        return ds;
    }


    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("selectClass"))
        {
            GridView2.EditIndex = -1;
            floatBoxBg2.Visible = true;
            divSClass.Visible = true;
            hdfeditflag.Value = "1";
        }
        if (e.CommandName.Equals("selectTeacher"))
        {
            GridView2.EditIndex = -1;
            floatBoxBg1.Visible = true;
            divSTeacher.Visible = true;
            hdfeditflag.Value = "1";
        }
        if (e.CommandName.Equals("selectCity"))
        {
            GridView2.EditIndex = -1;
            floatBoxBg3.Visible = true;
            divSCity.Visible = true;
            hdfeditflag.Value = "1";
        }
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("selectClass"))
        {
            GridView1.EditIndex = -1;
            floatBoxBg2.Visible = true;
            divSClass.Visible = true;
            hdfeditflag.Value = "0";
        }
        if (e.CommandName.Equals("selectTeacher"))
        {
            GridView1.EditIndex = -1;
            floatBoxBg1.Visible = true;
            divSTeacher.Visible = true;
            hdfeditflag.Value = "0";
        }
        if (e.CommandName.Equals("selectCity"))
        {
            GridView1.EditIndex = -1;
            floatBoxBg3.Visible = true;
            divSCity.Visible = true;
            hdfeditflag.Value = "0";
        }
        GridviewControl.ResetGridView(this.GridView1);
    }
    protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView2.EditIndex = -1;
        GridView2.DataSource = dsRecord;
        GridView2.DataBind();
        GridviewControl.ResetGridView(this.GridView1);
    }
    protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView2.EditIndex = e.NewEditIndex;
        GridView2.DataSource = dsRecord;
        GridView2.DataBind();
        GridviewControl.ResetGridView(this.GridView1);
    }
    protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        HiddenField hdfRecordId = (HiddenField)GridView2.Rows[e.RowIndex].FindControl("hdfRecordId");
        string newrecordid = hdfRecordId.Value;
        for (int j = 0; j < dsRecord.Tables[0].Rows.Count; j++)
        {
            string rid=dsRecord.Tables[0].Rows[j]["RecordId"].ToString();
            if ( rid== newrecordid)
            {
                dsRecord.Tables[0].Rows.Remove(dsRecord.Tables[0].Rows[j]);
                break;
            }
        }
        ViewState["tbRecord"] = dsRecord;
        GridView2.DataSource = dsRecord;
        GridView2.DataBind();
        if (GridView2.Rows.Count == 0)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("RecordId");
            dt.Columns.Add("ClassTypeName");
            dt.Columns.Add("ClassCode");
            dt.Columns.Add("ClassName");
            dt.Columns.Add("ClassId");
            dt.Columns.Add("Satisficing");
            dt.Columns.Add("TeacherName");
            dt.Columns.Add("TeacherCode");
            dt.Columns.Add("TeacherTypeId");
            dt.Columns.Add("TeacherTypeName");
            dt.Columns.Add("EmpLevelId");
            dt.Columns.Add("EmpLevelName");
            dt.Columns.Add("Time");
            dt.Columns.Add("OpenTime");
            dt.Columns.Add("FactTimes");
            dt.Columns.Add("CenterName");
            dt.Columns.Add("CenterId");
            dt.Columns.Add("CityId");
            dt.Columns.Add("CityName");

            dt.Columns.Add("");

            GridviewControl.GridViewDataBind(this.GridView2, dt);

        }
        GridviewControl.ResetGridView(this.GridView1);
        GridviewControl.ResetGridView(this.GridView2);
    }
    protected void GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int RecordId = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Values[0].ToString());
        int classid = Convert.ToInt32(((HiddenField)GridView2.Rows[e.RowIndex].FindControl("hdfClassId")).Value);
        int centerId = Convert.ToInt32(((HiddenField)GridView2.Rows[e.RowIndex].FindControl("hdfCenterId")).Value);
        int cityId = Convert.ToInt32(((HiddenField)GridView2.Rows[e.RowIndex].FindControl("hdfcityId")).Value);
        int TeacherTypeId = Convert.ToInt32(((DropDownList)GridView2.Rows[e.RowIndex].FindControl("ddlTeacherType")).SelectedValue);
        int EmpLevelId = Convert.ToInt32(((DropDownList)GridView2.Rows[e.RowIndex].FindControl("ddlEmpLevel")).SelectedValue);
        int Satisficing = -1;
        string TeacherName = "";
        decimal time = 0;
        int FactTimes = 0;
        string opentime = "";
        string teachercode = "";
       
        TextBox cSatisficing = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtSatisficing");
        TextBox cTeacherName = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtTeacherName");
        TextBox cTeacherCode = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtTeacherCode");
        TextBox ctime = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtTime");
        TextBox copentime = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtOpenTime");
        TextBox cFactTimes = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtFactTimes");
        TextBox cClassCode = (TextBox)GridView2.Rows[e.RowIndex].FindControl("txtClassCode");
        
        string ClassTypeName = ((Label)GridView2.Rows[e.RowIndex].FindControl("lblClassTypeName")).Text;
        string ClassName = ((Label)GridView2.Rows[e.RowIndex].FindControl("lblClassName")).Text;
        string ClassCode = ((TextBox)GridView2.Rows[e.RowIndex].FindControl("txtClassCode")).Text;
        string EmpLevelName= ((DropDownList)GridView2.Rows[e.RowIndex].FindControl("ddlEmpLevel")).SelectedItem.Text;
        string TeacherTypeName =((DropDownList)GridView2.Rows[e.RowIndex].FindControl("ddlTeacherType")).SelectedItem.Text;
        string CityName = ((TextBox)GridView2.Rows[e.RowIndex].FindControl("txtcityName")).Text;
        string CenterName = ((Label)GridView2.Rows[e.RowIndex].FindControl("lblCenterName")).Text;
        if (classid==0)
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请填写选择课程！"), false);
        }
        else if (cityId == 0)
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请填写选择开课城市！"), false);
        }
        else if (cSatisficing.Text == "")
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请填写课程满意程度！"), false);
            cSatisficing.Focus();
        }
        else if (!Regex.IsMatch(cSatisficing.Text.ToString(), "^(?:0|[1-9][0-9]?|100)$"))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("课程满意程度只能为0到100的正数！"), false);
            cSatisficing.Focus();
        }
        else
            Satisficing = Convert.ToInt32(cSatisficing.Text.Trim());

        if (cTeacherName.Text == "")
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请填写导师姓名！"), false);
            cTeacherName.Focus();
        }
        else if (!CoreTools.ProcessSqlStr(cTeacherName.Text, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
        }
        else
            TeacherName = cTeacherName.Text.Trim();
        if (cTeacherCode.Text == "")
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请填写导师编号或选择导师！"), false);
            cTeacherCode.Focus();
        }
        else if (!CoreTools.ProcessSqlStr(cTeacherCode.Text, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
        }
        else
            teachercode = cTeacherCode.Text.Trim();
        if (ctime.Text.Trim() == "" || ctime.Text.Trim()=="0")
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请填写课程小时数！"), false);
            ctime.Focus();
        }
        else
            time = Convert.ToDecimal(ctime.Text.Trim());
        if (cFactTimes.Text == "")
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请填写培训人次！"), false);
            cFactTimes.Focus();
        }
        else if (!Regex.IsMatch(cFactTimes.Text.ToString(), "^[0-9]*[1-9][0-9]*$"))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("培训人次必须为正整数！"), false);
            cFactTimes.Focus();
        }
        else
            FactTimes = Convert.ToInt32(cFactTimes.Text.Trim());
        if (copentime.Text == "")
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请填写开课时间！"), false);
            copentime.Focus();
        }
        else if (!Regex.IsMatch(copentime.Text.ToString(), "^([2-9]\\d{3})(0[1-9]|1[0-2])$"))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("开课时间格式不正确,正确格式形如：201206！"), false);
            copentime.Focus();
        }
        else
            opentime = copentime.Text.Trim();


        if (ClassCode!=""&&Satisficing != -1 && TeacherName != "" && teachercode != "" && time != 0 && opentime != "" && FactTimes != 0&&cityId!=0)
        {
            
            for (int i = 0; i < dsRecord.Tables[0].Rows.Count; i++)
            {
                int newrecordid=Convert.ToInt32(dsRecord.Tables[0].Rows[i]["RecordId"].ToString());
                if (newrecordid == RecordId)
                {
                    DataRow RecordTableRow = dsRecord.Tables[0].Rows[i];
                    RecordTableRow["ClassId"] = classid;
                    RecordTableRow["ClassTypeName"] = ClassTypeName;
                    RecordTableRow["ClassCode"] = ClassCode;
                    RecordTableRow["ClassName"] = ClassName;
                    RecordTableRow["EmpLevelId"] = EmpLevelId;
                    RecordTableRow["EmpLevelName"] = EmpLevelName;

                    RecordTableRow["Satisficing"] = Satisficing;
                    RecordTableRow["TeacherName"] = TeacherName;
                    RecordTableRow["Time"] = time;
                    RecordTableRow["FactTimes"] = FactTimes;
                    RecordTableRow["CenterId"] = centerId;
                    RecordTableRow["OpenTime"] = opentime;
                    RecordTableRow["TeacherTypeId"] = TeacherTypeId;
                    RecordTableRow["TeacherCode"] = teachercode;
                    RecordTableRow["TeacherTypeName"] = TeacherTypeName;
                    RecordTableRow["CityName"] = CityName;
                    RecordTableRow["CenterName"] = CenterName;
                    RecordTableRow["CityId"] = cityId;
                }
            }
            GridView2.EditIndex = -1;
            GridView2.DataSource = dsRecord;
            GridView2.DataBind();
            selectAll.Enabled = true;
            Button1.Enabled = true;
            btnMass_Add.Enabled = true;
        }
        GridviewControl.ResetGridView(this.GridView1);
    }
    protected void GetHaveSetData()
    {
        DropDownList ddlEmpLevel = (DropDownList)GridView2.Rows[GridView2.EditIndex].FindControl("ddlEmpLevel");
        DropDownList ddlTeacherType = (DropDownList)GridView2.Rows[GridView2.EditIndex].FindControl("ddlTeacherType");
        TextBox txtTeacherCode = (TextBox)GridView2.Rows[GridView2.EditIndex].FindControl("txtTeacherCode");
        TextBox txtTeacherName = (TextBox)GridView2.Rows[GridView2.EditIndex].FindControl("txtTeacherName");
        TextBox txtTime = (TextBox)GridView2.Rows[GridView2.EditIndex].FindControl("txtTime");
        TextBox txtSatisficing = (TextBox)GridView2.Rows[GridView2.EditIndex].FindControl("txtSatisficing");
        TextBox txtFactTimes = (TextBox)GridView2.Rows[GridView2.EditIndex].FindControl("txtFactTimes");
        TextBox txtOpenTime = (TextBox)GridView2.Rows[GridView2.EditIndex].FindControl("txtOpenTime");
        if (ddlEmpLevel != null)
            emplevelId = ddlEmpLevel.SelectedValue;
        if (ddlTeacherType != null)
            teacherTypeId = ddlTeacherType.SelectedValue;
        if (txtTeacherCode != null)
            TeacherCode = txtTeacherCode.Text;
        if (txtTeacherName != null)
            TeacherName = txtTeacherName.Text;
        if (txtTime != null)
            Time = txtTime.Text;
        if (txtSatisficing != null)
            Satisficing = txtSatisficing.Text;
        if (txtFactTimes != null)
            FactTimes = txtFactTimes.Text;
        if (txtOpenTime != null)
            OpenTime = txtOpenTime.Text;
        

    }
    protected  void SetHaveSetData()
    {
        DropDownList ddlEmpLevel = (DropDownList)GridView2.Rows[GridView2.EditIndex].FindControl("ddlEmpLevel");
        DropDownList ddlTeacherType = (DropDownList)GridView2.Rows[GridView2.EditIndex].FindControl("ddlTeacherType");
        TextBox txtTeacherCode = (TextBox)GridView2.Rows[GridView2.EditIndex].FindControl("txtTeacherCode");
        TextBox txtTeacherName = (TextBox)GridView2.Rows[GridView2.EditIndex].FindControl("txtTeacherName");
        TextBox txtTime = (TextBox)GridView2.Rows[GridView2.EditIndex].FindControl("txtTime");
        TextBox txtSatisficing = (TextBox)GridView2.Rows[GridView2.EditIndex].FindControl("txtSatisficing");
        TextBox txtFactTimes = (TextBox)GridView2.Rows[GridView2.EditIndex].FindControl("txtFactTimes");
        TextBox txtOpenTime = (TextBox)GridView2.Rows[GridView2.EditIndex].FindControl("txtOpenTime");
        if (ddlEmpLevel != null)
            ddlEmpLevel.SelectedValue = emplevelId;
        if (ddlTeacherType != null)
            ddlTeacherType.SelectedValue=teacherTypeId;;
        if (txtTeacherCode != null)
             txtTeacherCode.Text=TeacherCode;
        if (txtTeacherName != null)
            txtTeacherName.Text = TeacherName;
        if (txtTime != null)
            txtTime.Text = Time;
        if (txtSatisficing != null)
            txtSatisficing.Text = Satisficing;
        if (txtFactTimes != null)
            txtFactTimes.Text = FactTimes;
        if (txtOpenTime != null)
            txtOpenTime.Text = OpenTime;
        
    }
}